home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / TECHNICA / COMPUTER / H254.ZIP / IRITSM3S.ZIP / POLY3D / POSTSCRP.C < prev    next >
C/C++ Source or Header  |  1992-01-02  |  6KB  |  194 lines

  1. /*****************************************************************************
  2. * Module to handle PostScript output.                         *
  3. *                                         *
  4. * Written by:  Gershon Elber                Ver 1.0, Apr. 1990   *
  5. *****************************************************************************/
  6.  
  7. #ifdef __MSDOS__
  8. #include <sys\types.h>
  9. #else
  10. #include <sys/types.h>
  11. #endif
  12.  
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include <time.h>
  16. #include "program.h"
  17. #include "genmat.h"
  18. #include "interact.h"
  19. #include "graphgen.h"
  20.  
  21. static FILE *PSFile = NULL;           /* Used to save PostScript text file. */
  22.  
  23. static void (* SavedMoveToPtr)(RealType Coord[3]);
  24. static void (* SavedDrawToPtr)(RealType Coord[3]);
  25. static void PSMoveTo(RealType Coord[3]);
  26. static void PSDrawTo(RealType Coord[3]);
  27.  
  28. /*****************************************************************************
  29. * Routine to save the current view as PostScript text file.             *
  30. * Although this routine dumps EPSF-1.2 compatible code, it still dumps       *
  31. * the 'showpage' command. The 'showpage' is allowed by the EPSF document and *
  32. * it is the responsibility of the including program to disable showpage.     *
  33. *****************************************************************************/
  34. void SavePostScript(IPObjectStruct *Objects)
  35. {
  36.     int i;
  37.     time_t time1970 = time(NULL); 
  38.     char *p, FileName[PATH_NAME_LEN];
  39.     static char FileCount = '0';
  40.  
  41.     static char *PSHeader[] = {
  42.     "%!PS-Adobe-2.0 EPSF-1.2",
  43.     "%%BoundingBox: 36 108 540 612",    /* 7x7 inches, LL at [0.5, 1.5]. */
  44.     "%%Title:",
  45.     "%%Creator: Poly3d",
  46.     "%%CreationDate:",
  47.     "%%EndComments",
  48.     "",
  49.     "gsave",
  50.     "",
  51.     "72 72 scale",    /* First scale to inches, se we can speak in inches. */
  52.     "4.0 5.0 translate",             /* To Center of image area. */
  53.     "3.5 3.5 scale",     /* Make the universe -1..1 on both X and Y. */
  54.     "",
  55.     "/line {",
  56.     "    newpath",
  57.     "    moveto",
  58.     "    lineto",
  59.     "    stroke",
  60.     "} def",
  61.     "",
  62.     "/setdashline {",
  63.     "    [0.012 0.024] 0 setdash",
  64.     "} def",
  65.     "",
  66.     "/setfullline {",
  67.     "    [] 0 setdash",
  68.     "} def",
  69.     "",
  70.     "0.006 setlinewidth",        /* Set some default line attributes. */
  71.     "1 setlinecap",
  72.     "1 setlinejoin",
  73.     "setfullline",
  74.     "",
  75.     NULL
  76.     };
  77.     static char *PSTrailer[] = {
  78.     "",
  79.     "showpage",
  80.     "grestore",
  81.     NULL
  82.     };
  83.  
  84.     strcpy(FileName, GENERIC_PS_FILE);
  85.     if ((p = strchr(FileName, '#')) != NULL) {
  86.     *p = FileCount;
  87.     if (FileCount++ == '9') FileCount = '0';
  88.     }
  89.     if ((PSFile = fopen(FileName, "wt")) == NULL) {
  90.     GGTone(700, 200);
  91.     return;
  92.     }
  93.  
  94.     for (i = 0; PSHeader[i] != NULL; i++) {
  95.     if (strcmp(PSHeader[i], "%%Title:") == 0)
  96.         fprintf(PSFile, "%s %s\n", PSHeader[i], GlblFirstDataFileName);
  97.     else if (strcmp(PSHeader[i], "%%CreationDate:") == 0)
  98.         fprintf(PSFile, "%s %s", PSHeader[i], ctime(&time1970));
  99.     else
  100.         fprintf(PSFile, "%s\n", PSHeader[i]);
  101.     }
  102.  
  103.     SavedMoveToPtr = MoveToPtr;
  104.     SavedDrawToPtr = DrawToPtr;
  105.     MoveToPtr = PSMoveTo;
  106.     DrawToPtr = PSDrawTo;
  107.     DrawAllObjects(Objects);
  108.     MoveToPtr = SavedMoveToPtr;
  109.     DrawToPtr = SavedDrawToPtr;
  110.  
  111.     for (i = 0; PSTrailer[i] != NULL; i++)
  112.     fprintf(PSFile, "%s\n", PSTrailer[i]);
  113.  
  114.     fclose(PSFile);
  115. }
  116.  
  117. static RealType LastCoord[3];  /* Used to store last point we moved/draw to. */
  118.  
  119. /*****************************************************************************
  120. * Routine to mave to 3D    point given as Coord[3], using Mat transform.         *
  121. *****************************************************************************/
  122. static void PSMoveTo(RealType Coord[3])
  123. {
  124.     MultVecby4by4(LastCoord, Coord, CrntViewMat);   /* Set last point coord. */
  125. }
  126.  
  127. /*****************************************************************************
  128. * Routine to draw to 3D    point given as Coord[3], using Mat transform, from   *
  129. * the last point we moved to.                             *
  130. *****************************************************************************/
  131. static void PSDrawTo(RealType Coord[3])
  132. {
  133.     static DashState = FALSE;
  134.     RealType NewCoord[3], MiddleCoord[3], t;
  135.  
  136.     MultVecby4by4(NewCoord, Coord, CrntViewMat);    /* Set last point coord. */
  137.     if (GlblClosedObject && NewCoord[2] < LastCoord[2]) {
  138.     GEN_COPY(LastCoord, NewCoord, 3 * sizeof(RealType));
  139.     return;
  140.     }
  141.  
  142.     /* Implementation of simple depth cue - if line is >Z or <Z ... */
  143.     if (LastCoord[2] <= 0.0 && NewCoord[2] <= 0.0) {
  144.     if (GlblDepthCue && !DashState) {
  145.         fprintf(PSFile, "setdashline\n");
  146.         DashState = TRUE;
  147.     }
  148.     fprintf(PSFile, "%10.7lf %10.7lf  ", LastCoord[0], LastCoord[1]);
  149.     fprintf(PSFile, "%10.7lf %10.7lf line\n", NewCoord[0], NewCoord[1]);
  150.     }
  151.     else if (LastCoord[2] >= 0.0 && NewCoord[2] >= 0.0 ||
  152.     ABS(LastCoord[2] - NewCoord[2]) < EPSILON) {
  153.     if (GlblDepthCue && DashState) {
  154.         fprintf(PSFile, "setfullline\n");
  155.         DashState = FALSE;
  156.     }
  157.     fprintf(PSFile, "%10.7lf %10.7lf  ", LastCoord[0], LastCoord[1]);
  158.     fprintf(PSFile, "%10.7lf %10.7lf line\n", NewCoord[0], NewCoord[1]);
  159.     }
  160.     else {                      /* Line intersect Z = 0 plane. */
  161.     t = LastCoord[2] / (LastCoord[2] - NewCoord[2]);
  162.     MiddleCoord[0] = LastCoord[0] * (1.0 - t) + NewCoord[0] * t;
  163.     MiddleCoord[1] = LastCoord[1] * (1.0 - t) + NewCoord[1] * t;
  164.  
  165.     if (GlblDepthCue && DashState) {
  166.         fprintf(PSFile, "setfullline\n");
  167.         DashState = FALSE;
  168.     }
  169.     if (LastCoord[2] > 0.0) {
  170.         fprintf(PSFile, "%10.7lf %10.7lf  ", LastCoord[0], LastCoord[1]);
  171.         fprintf(PSFile, "%10.7lf %10.7lf line\n", MiddleCoord[0], MiddleCoord[1]);
  172.     }
  173.     else {
  174.         fprintf(PSFile, "%10.7lf %10.7lf  ", MiddleCoord[0], MiddleCoord[1]);
  175.         fprintf(PSFile, "%10.7lf %10.7lf line\n", NewCoord[0], NewCoord[1]);
  176.     }
  177.  
  178.     if (GlblDepthCue && !DashState) {
  179.         fprintf(PSFile, "setdashline\n");
  180.         DashState = TRUE;
  181.     }
  182.     if (LastCoord[2] < 0.0) {
  183.         fprintf(PSFile, "%10.7lf %10.7lf  ", LastCoord[0], LastCoord[1]);
  184.         fprintf(PSFile, "%10.7lf %10.7lf line\n", MiddleCoord[0], MiddleCoord[1]);
  185.     }
  186.     else {
  187.         fprintf(PSFile, "%10.7lf %10.7lf  ", MiddleCoord[0], MiddleCoord[1]);
  188.         fprintf(PSFile, "%10.7lf %10.7lf line\n", NewCoord[0], NewCoord[1]);
  189.     }
  190.     }
  191.  
  192.     GEN_COPY(LastCoord, NewCoord, 3 * sizeof(RealType));    /* Current point. */
  193. }
  194.